import {Component, ViewChild} from '@angular/core';
import {Content, InfiniteScroll, IonicPage, NavController, NavParams} from 'ionic-angular';
import {PagingConfig} from "@providers/paging";
import {NativeProvider} from "@providers/natives/native";
import {ConsigneeProvider} from "@providers/interface/consignee";
import {StorageProvider} from "@providers/storage";
import {Util} from "@providers/util";
import {DialogProvider} from "@providers/dialog";

/**
 * Generated class for the ConsigneeWaitPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-consignee-wait',
  templateUrl: 'consignee-wait.html',
})
export class ConsigneeWaitPage {

  @ViewChild(Content) content: Content;

  datas: any[] = [];//待提货单据
  userInfo: any = this.storage.read("userInfo");
  datasConfig: PagingConfig = new PagingConfig();//待提货分页
  isScrollDown = false;//判断页面是否是向下滑动
  serch;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private native: NativeProvider,
              private storage: StorageProvider,
              private consignee: ConsigneeProvider,
              private dialog: DialogProvider,) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ConsigneeWaitPage');
  }

  ionViewWillEnter() {
    this.getWait();
  }

  scanLading() {
    this.native.openScanner().then((barcodeData) => {
      if (Util.isNull(barcodeData)) return;
      this.queryLadingWaiting(barcodeData)
    }).catch(() => {
    });
  }

  getLadingWait(ID?, CS?, fn?: Function, error?: Function) {

    this.consignee.getLadingWait(
      {
        ID: ID,
        THYBM: this.userInfo.PARTYID,
        THYMC: this.userInfo.USERNAME,
        CS: CS || 0
      }
    ).then((data) => {
      fn(data.data);
    }).catch((error) => {
      error(error);
    });

  }

  queryLadingWait(ID?) {
    this.getLadingWait(ID, 2, (data) => {
      this.getWait();
    }, (error) => {
      this.dialog.showAlert('获取提货单失败' + error);
    })
  }

  queryLadingWaiting(ID?) {
    this.getLadingWait(ID, 2, (data) => {
      this.getWait();
    }, (error) => {
      this.dialog.showAlert('获取提货单失败' + error);
    })
  }


  setLading(ID?, LINE?) {
    this.dialog.showConfirm(() => {
      this.consignee.takeGoods({
        ID: ID,
        THYBM: this.userInfo.PARTYID,
        THYMC: this.userInfo.USERNAME,
        LINE: LINE
      }).then((data) => {
        //this.datas.push.apply(data);
        this.getWait();
      }).catch((error) => {
        this.dialog.showAlert('提货确认失败' + error);
      })
    }, '是否已提货？', '提货确认');
  }

  /**
   * 获取数据
   * @param DJZT=CS 状态 CS=0表示已提货未装车 CS=1表示已提货已装车 CS=2表示待提货
   * @param pagingConfig 分页
   * @param fn 方法
   * @param ID 根据单据编号查询
   * @param errorFun 调用失败的方法
   */
  private getData(fn: (value) => void, pagingConfig, DJZT?: string,ID?:string,errorFun?: (error) => void) {
    if (pagingConfig.isEnd) {
      return;
    }
    this.consignee.getLadingFinish({
      THYBM: this.userInfo.PARTYID,
      THYMC: this.userInfo.USERNAME,
      CS: DJZT,
      ID:ID
    }, pagingConfig)
      .then((data) => {
        fn(data);
        console.info(data);
      }).catch((error) => {
        errorFun(error)
    });
  }

  getWait() {
    this.datas = [];
    this.datasConfig = new PagingConfig();
    if (this.datas.length == 0) {
      this.getData((value) => {
        this.datas = value;
        // console.log(this.datas);
      }, this.datasConfig, '2',this.serch);
    }
  }

  getMoreWaitDatas(infiniteScroll: InfiniteScroll) {
    this.getData((value) => {
      infiniteScroll.complete();
      if (this.datasConfig.isEnd) {
        infiniteScroll.enable(false);
      }
      this.datas.push.apply(this.datas, value);
    }, this.datasConfig, '2',this.serch);
  }

  /**
   * 刷新数据
   */
  refresh(refresher?: any) {
    this.datasConfig = new PagingConfig();
    this.getData(data => {
      this.datas = data;
      if (refresher) {
        setTimeout(() => {
          refresher.complete();
        }, 500);
      }
    }, this.datasConfig, '2',this.serch,error => {
      if (refresher) {
        setTimeout(() => {
          refresher.complete();
        }, 500);
      }
    });
  }

  /**
   * 滑动时触发
   */
  scrollContent() {
    this.isScrollDown = this.content.directionY == "down";
  }

}
